package customerview;

import check.BoxRenderer;
import check.CheckBoxEditor;
import check.CheckBoxRenderer;
import users.LoginFun;
import users.ManagerFun;
import users.UsersFun;
import View.View;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

//购物车
public class ShoppingCart extends JFrame {
    String username;
    public ShoppingCart(String username){
        this.username=username;
        //初始化界面
        initJFrame();
        //在这个界面添加内容
        initView();
        //让页面展示出来
        this.setVisible(true);

    }

    private void initView() {

        //添加搜索输入框
        JTextField searchText = new JTextField("（请输入图书编号）", 10);
        searchText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                if (searchText.getText().equals("（请输入图书编号）")) {
                    searchText.setText(""); // 清空文本框供用户输入
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (searchText.getText().isEmpty()) {
                    searchText.setText("（请输入图书编号）"); // 用户未输入内容时恢复提示信息
                }
            }
        });
        searchText.setBounds(30,10,130,30);
        this.getContentPane().add(searchText);

        // 创建一个3行3列的默认数据模型
        DefaultTableModel model = new DefaultTableModel();

        // 如果需要可以给表格添加列名
        model.setColumnIdentifiers(new String[]{"选择","图书编号","书名", "类型","出版社","作者","单价","购买数量"});

        // 创建一个JTable实例并应用模型
        JTable table = new JTable(model){
            // 重写isCellEditable方法，使所有单元格默认不可编辑，但"EditableColumn2"列的单元格可编辑
            @Override
            public boolean isCellEditable(int row, int column) {
                // 假设我们想要"EditableColumn2"列的单元格可编辑
                // 注意：列索引是从0开始的，所以"EditableColumn2"的索引是1
                return column==0; // 允许第2列（索引为1）的单元格可编辑
            }
        };
        BoxRenderer centeredRenderer = new BoxRenderer();
        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setCellRenderer(centeredRenderer);
        }
        // 可选：使用自定义渲染器或编辑器，例如复选框
        table.getColumnModel().getColumn(0).setCellRenderer(new CheckBoxRenderer());
        table.getColumnModel().getColumn(0).setCellEditor(new CheckBoxEditor((new JCheckBox())));

        // 使用JScrollPane包装表格以自动处理滚动条
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBounds(20, 50, 660, 350); // 设置滚动面板的位置和大小
        this.getContentPane().add(scrollPane); // 将滚动面板添加到窗体的内容窗格中

        //添加查询按钮
        JButton searchButton=new JButton();
        searchButton.setBounds(170,10,80,30);
        searchButton.setText("查询");
        this.getContentPane().add(searchButton);
        //添加监听
        //.....
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    UsersFun.cartone(searchText.getText(),username, model);
                    LoginFun.showJDialog("查询成功！");
                } catch (Exception ex) {
                    LoginFun.showJDialog("查询失败！请重新输入！");
                    //throw new RuntimeException(ex);
                }
            }
        });

        JButton searchButton2=new JButton();
        searchButton2.setBounds(330,10,110,30);
        searchButton2.setText("查询全部");
        this.getContentPane().add(searchButton2);
        searchButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    UsersFun.allcart(username,model);
                    LoginFun.showJDialog("查询成功！");
                } catch (Exception ex) {
                    LoginFun.showJDialog("查询失败！");
                    //throw new RuntimeException(ex);
                }
            }
        });

        //添加返回主界面按钮
        JButton ReturnButton=new JButton();
        ReturnButton.setBounds(530,10,130,30);
        ReturnButton.setText("返回主界面");
        this.getContentPane().add(ReturnButton);
        //添加监听
        ReturnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ShoppingCart.this.setVisible(false);
                new View(username);
            }
        });

        // 添加监听代码...

        //添加直接购买按钮
        JButton purchaseButton=new JButton();
        purchaseButton.setBounds(540,410,130,30);
        purchaseButton.setText("直接购买");
        this.getContentPane().add(purchaseButton);
        //添加监听
        //.....
        purchaseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    UsersFun.purchase(table,username);
                    LoginFun.showJDialog("购买成功！");
                } catch (Exception ex) {
                    LoginFun.showJDialog("购买失败！库存不足！");
                    //throw new RuntimeException(ex);
                }
            }
        });
        //删除购物车
        JButton deleteButton=new JButton();
        deleteButton.setBounds(340,410,130,30);
        deleteButton.setText("删除");
        this.getContentPane().add(deleteButton);
        //添加监听
        //.....
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    UsersFun.deletecart(table,username);
                    UsersFun.allcart(username,model);
                    LoginFun.showJDialog("删除成功！");
                } catch (Exception ex) {
                    LoginFun.showJDialog("删除失败！");
                    //throw new RuntimeException(ex);
                }
            }
        });
        try{
            UsersFun.allcart(username,model);
        }catch (Exception e){
            LoginFun.showJDialog("购物车信息初始化失败！");
            //throw new RuntimeException(e);
        }

    }

    private void initJFrame() {
        this.setTitle("我的购物车");
        this.setSize(700, 500);
        //设置页面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //取消按钮等元素居中放置
        this.setLayout(null);
    }
}

